﻿#include "feedback.h"
#include "ui_feedback.h"

Feedback::Feedback(QWidget *parent) :
    BaseEditor(parent),
    ui(new Ui::Feedback)
{
    ui->setupUi(this);

    table=TD_FEEDBACK;
    selectId=0;
    listModel=new QStringListModel(this);
    ui->listView->setModel(listModel);

    model=new QSqlTableModel(this);
    ui->tableView->setModel(model);

    updateCategory();
    createMenu();
}

Feedback::~Feedback()
{
    delete ui;
}

void Feedback::generateMenu()
{
    menu->addAction(QStringLiteral("新记录... (F1)"),this,&Feedback::newItem);
    menu->addSeparator();
    menu->addAction(QStringLiteral("更新类别 (F4)"),this,&Feedback::updateCategory);
}

void Feedback::keyPressEvent(QKeyEvent *event)
{
    switch (event->key()) {
    case Qt::Key_F1:
        newItem();
        break;

    case Qt::Key_F4:
        updateCategory();
        break;
    }
}

void Feedback::updateCategory()
{
    if(!categories.isEmpty())
        categories.clear();

    categories=sp_getCategory(table);
    listModel->setStringList(categories);
}

void Feedback::updateView()
{
    if(!selectCategory.isEmpty())
    {
        model->setTable(table);
        model->setFilter(QString("category = '%1'").arg(selectCategory));
        model->select();

        ui->tableView->hideColumn(0);   //id
        ui->tableView->hideColumn(1);   //month_
        model->setHeaderData(2,Qt::Horizontal,QStringLiteral("日期"));    //date_
        ui->tableView->hideColumn(3);   //category
        model->setHeaderData(4,Qt::Horizontal,QStringLiteral("事由"));    //detail
        // ui->tableView->setColumnWidth(4,480);
        ui->tableView->hideColumn(5);
        // model->setHeaderData(5,Qt::Horizontal,QStringLiteral("结果"));    //addition
    }
}

void Feedback::newItem()
{
    FB_AddItem itemAdd;
    if(!selectCategory.isEmpty())
    {
        itemAdd.setTopic(selectCategory);
    }
    itemAdd.move(x() + 100,y() + 50);
    if(itemAdd.exec() == QDialog::Accepted)
    {
        updateCategory();
        if(categories.contains(selectCategory))
            updateView();
    }
}

void Feedback::on_listView_doubleClicked(const QModelIndex &index)
{
    if(index.isValid())
    {
        selectCategory=index.data().toString();
        updateView();
    }
}

void Feedback::on_tableView_clicked(const QModelIndex &index)
{
    if(index.isValid())
    {
        selectId=sp_fetchId(index);
        previousDetail=sp_fetchString(index,4);
        ui->detail->setPlainText(previousDetail);
    }
}

void Feedback::on_tableView_doubleClicked(const QModelIndex &index)
{
    if(previousDetail.isEmpty())
        return;

    QString date=sp_fetchString(index,1);
    QString detail=date + " " + previousDetail;
    if(qApp->clipboard()->text() != detail)
        qApp->clipboard()->setText(detail);
}

void Feedback::on_btn_saveDetail_clicked()
{
    if(selectId <= 0)
        return;

    QString detail=ui->detail->toPlainText();
    if(detail.isEmpty() || detail == previousDetail)
        return;

    if(sp_updateColumnById(table,"detail",detail,selectId))
        updateView();
    else
        MESSAGE_CANNOT_SAVE
}

/*
void Feedback::on_btn_saveAddition_clicked()
{
    if(selectId <= 0)
        return;

    QString addition=ui->addition->toPlainText();
    if(addition.isEmpty() || addition==previousAddition)
        return;

    if(sp_updateColumnById(table,"addition",addition,selectId))
        updateView();
    else
        MESSAGE_CANNOT_SAVE
}
*/
